﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetworkAnalysis
{
    class InitialLicense
    {
        private InitialLicense() { }

        private static InitialLicense initialLicense = new InitialLicense();

        public static InitialLicense getInstance()
        {
            return initialLicense;
        }

        public bool iniAELicenseControlObj()
        {
            ESRI.ArcGIS.esriSystem.IAoInitialize AoIni = new AoInitializeClass();
            bool Successful = false; //判断是否初始化成功
            esriLicenseProductCode production = esriLicenseProductCode.esriLicenseProductCodeAdvanced;
            esriLicenseStatus status = AoIni.IsProductCodeAvailable(production);
            if (status == esriLicenseStatus.esriLicenseAvailable)
            {
                status = AoIni.Initialize(production);
                if (status == esriLicenseStatus.esriLicenseAlreadyInitialized || status == esriLicenseStatus.esriLicenseCheckedOut)
                {
                    //NetWork Analysis
                    esriLicenseExtensionCode NAExtension = esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork;
                    esriLicenseStatus NAStatus = AoIni.IsExtensionCodeAvailable(production, NAExtension);
                    if (NAStatus == esriLicenseStatus.esriLicenseAvailable)
                    {
                        NAStatus = AoIni.CheckOutExtension(NAExtension);
                    }

                    if (NAStatus == esriLicenseStatus.esriLicenseCheckedOut)
                    {
                        Successful = true;
                    }
                }

            }
            return Successful;
        }

        public void ShutDown()
        {
            ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown();
        }
    }
}
